# coding: utf8

import json


class Preliminary:

    @staticmethod
    def pyobj_to_json():
        """Python类型存储为JSON格式数据示例"""

        class MyEnum:
            RED = 1
            BLUE = 2.5
            GREEN = 3

        j1 = {"WeekDay1": "January", "WeekDay2": "February"}
        j2 = [1, 2, 3]
        j3 = (1, 2)
        j4 = "热爱祖国"
        j5 = -123.5
        j6 = 12E3
        j7 = 12345
        j8 = (False, True, None)
        j9 = {"a": [1, 2], "b": {"1": "歌唱祖国"}, "c": MyEnum.BLUE}

        js = {"dict": j1, "list": j2, "tuple": j3,
              "str": j4, "float": j5, "float_e": j6,
              "int": j7, "special_value": j8, "complex_object": j9}
        with open("stu53a.json", mode='wt') as f:
            json.dump(js, f)

    @staticmethod
    def json_to_pyobj():
        """将JSON格式数据读出为Python数据对象"""
        with open("stu53a.json", mode='rt') as f:
            json_data = json.load(f)
        for k, v in json_data.items():
            print("{}: type(v)={}, v={}".format(k, type(v), v))


def task():
    with open("stu53b.json", encoding='utf8') as fp:
        data = json.load(fp)

    for stu in data["students"]:
        if stu['age'] > 20:
            print(stu['name'], stu['age'])


def expand_train():
    with open("stu53b.json", encoding='utf8') as fp:
        data = json.load(fp)
    print(data)
    print(data['school'])
    for stu in data["students"]:
        if stu['age'] < 20:
            print(stu['name'], stu['age'])


if __name__ == '__main__':
    Preliminary.pyobj_to_json()
    Preliminary.json_to_pyobj()
    task()
    expand_train()

